<template>
  <div>
    <ComplexTable
      ref="postFromTable"
      :query-model="queryModel"
      :table-column-is-show="tableColumnIsShow"
      :create-permission="['sys:post:create']"
      @onSearch="search"
      @onAddPage="addPage"
    >
      <template v-slot:search_from_input>
        <el-form-item label="岗位名称" prop="name">
          <el-input
            v-model="queryModel.name"
            placeholder="请输入岗位名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="岗位编码" prop="code">
          <el-input
            v-model="queryModel.code"
            placeholder="请输入岗位编码"
            clearable
          />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-select v-model="queryModel.status" clearable placeholder="请选择岗位状态">
            <el-option
              v-for="(value,key,index) of getSystemDictionariesByType('USESTATUS')"
              :key="index"
              :label="value"
              :value="key"
            />
          </el-select>
        </el-form-item>
      </template>
      <template v-slot:table_column>
        <el-table-column
          type="selection"
          width="40"
        />
        <el-table-column
          v-if="tableColumnIsShow.name.isShow"
          prop="name"
          label="岗位名称"
          align="center"
          header-align="center"
        />
        <el-table-column
          v-if="tableColumnIsShow.code.isShow"
          prop="code"
          label="岗位编码"
          align="center"
          header-align="center"
        />
        <el-table-column
          v-if="tableColumnIsShow.sort.isShow"
          prop="sort"
          label="岗位排序"
          align="center"
          header-align="center"
        />
        <el-table-column
          v-if="tableColumnIsShow.status.isShow"
          prop="status"
          label="岗位状态"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            {{ getSystemDictionaries("USESTATUS",scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            <TableBtn :parameter="scope.row.id" :edit-permission="['sys:post:update' ]" :del-permission="['sys:post:delete' ]" @onDel="del" @onEditPage="editPage" />
          </template>
        </el-table-column>
      </template>
    </ComplexTable>

    <!--弹出框-->
    <MyEditDialog :dialog-visible="dialogVisible" :title="title" width="800px" @onDialogSave="save" @onGoBack="dialogVisible=false">
      <template v-slot:dialog_content>
        <EditForm v-if="dialogVisible" ref="EditForm" :form-model="editFormModel" :validate="formValidate" />
      </template>
    </MyEditDialog>
  </div>
</template>

<script>
import TableBtn from '@/components/TableBtn'
import ComplexTable from '@/components/ComplexTable'
import { pagePostREQ, getPostByIdREQ, updatePostREQ, createPostREQ, deletePostREQ } from '@/api/system/post'
import MyEditDialog from '@/components/MyEditDialog'
import { mapGetters } from 'vuex'
import EditForm from './EditForm'
import deepcopy from 'deepcopy'
import { addValidate, editValidate } from './validate'
export default {
  name: 'RoleIndex',
  components: { ComplexTable, TableBtn, MyEditDialog, EditForm },
  data() {
    return {
      queryModel: {
        name: '',
        code: '',
        status: ''
      },
      tableColumnIsShow: {
        name: { label: '岗位名称', isShow: true },
        code: { label: '岗位编号', isShow: true },
        sort: { label: '排序', isShow: true },
        status: { label: '状态', isShow: true }
      },
      page: null,
      title: '',
      dialogVisible: false,
      defaultEditForm: {
        name: '',
        code: '',
        sort: 0,
        remarks: '',
        status: 'NORMAL'
      },
      editFormModel: {},
      formValidate: null
    }
  },
  computed: {
    ...mapGetters({
      'getSystemDictionaries': 'dictionaries/getSystemDictionaries',
      'getSystemDictionariesByType': 'dictionaries/getSystemDictionariesByType'
    })
  },
  mounted() {

  },
  methods: {
    search(queryModel = {}) {
      pagePostREQ(queryModel).then((data) => {
        if (this.$refs['postFromTable']) { this.$refs['postFromTable'].setTableAndPage(data) }
      })
    },
    del(id) {
      if (id) {
        deletePostREQ(id).then(v => {
          this.search({ current: 1 })
        })
      }
    },
    editPage(id) {
      if (id) {
        this.title = '编辑岗位信息'
        getPostByIdREQ(id).then(data => {
          this.editFormModel = deepcopy(data)
          this.dialogVisible = true
          this.formValidate = editValidate
        })
      }
    },
    closeDialogVisible() {
      this.$refs['postFromTable'].onSearch()
      this.dialogVisible = false
    },
    save() {
      this.$refs['EditForm'].$refs['EditForm'].validate((valid) => {
        if (valid) {
          if (this.editFormModel.id) {
            updatePostREQ(this.editFormModel).then(v => {
              this.closeDialogVisible()
            })
          } else {
            createPostREQ(this.editFormModel).then(v => {
              this.closeDialogVisible()
            })
          }
        }
      })
    },
    addPage() {
      this.title = '新增岗位信息'
      this.editFormModel = deepcopy(this.defaultEditForm)
      this.dialogVisible = true
      this.formValidate = addValidate
    }
  }

}

</script>
<style lang='scss' scoped>
</style>
